package com.itheima.service;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.dao.RoleDao;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.pojo.Menu;
import com.itheima.pojo.Role;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service(interfaceClass = RoleService.class)
@Transactional
public class RoleServiceImpl implements RoleService {
    @Autowired
    private RoleDao roleDao;
    @Override
    public PageResult findPage(QueryPageBean queryPageBean) {

        Integer currentPage = queryPageBean.getCurrentPage();
        Integer pageSize = queryPageBean.getPageSize();
        String queryString = queryPageBean.getQueryString();
        //使用分页助手插件实现分页查询
        PageHelper.startPage(currentPage,pageSize);
        Page<Role> page = roleDao.findPage(queryString);
        return new PageResult(page.getTotal(),page.getResult());
    }

    @Override
    public void deleteByRoleId(Integer roleId) {
        roleDao.deleteAssocicationWithMenuByRoleId(roleId);
        roleDao.deleteAssocicationWithPermissionByRoleId(roleId);
        roleDao.deleteAssocicationWithUserByRoleId(roleId);
        roleDao.deleteByRoleId(roleId);
    }

    @Override
    public void add(Role role, Integer[] permissionIds, Integer[] menuIds) {
        roleDao.add(role);
        Integer roleId = role.getId();
        this.setAssociation(roleId,menuIds,permissionIds);
    }

    @Override
    public List<Menu> findAllMenu() {
        List<Menu> menus = roleDao.findAllMenu();
        return menus;
    }

    @Override
    public Role findById(Integer id) {
        return roleDao.findById(id);
    }

    @Override
    public List<Integer> findCheckMenuIdsByRoleId(Integer id) {
        return roleDao.findCheckMenuIdsByRoleId(id);
    }

    @Override
    public List<Integer> findCheckPermissionIdsByRoleId(Integer id) {
        return roleDao.findCheckPermissionIdsByRoleId(id);
    }

    @Override
    public void edit(Role role,Integer[] menuIds,Integer[] permissionIds) {
        roleDao.edit(role);
        Integer roleId = role.getId();
        roleDao.deleteAssocicationWithMenuByRoleId(roleId);
        roleDao.deleteAssocicationWithPermissionByRoleId(roleId);
        this.setAssociation(roleId,menuIds,permissionIds);
    }

    public void setAssociation(Integer roleId, Integer[] checkMenuIds, Integer[] checkPermissionIds) {
        if (checkMenuIds != null && checkMenuIds.length > 0) {
            for (Integer checkMenuId : checkMenuIds) {
                Map<String,Integer> map = new HashMap<>();
                map.put("role_id",roleId);
                map.put("menu_id",checkMenuId);
                roleDao.setMenuRole(map);
            }
        }
        if (checkPermissionIds != null && checkPermissionIds.length > 0) {
            for (Integer checkPermissionId : checkPermissionIds) {
                Map<String,Integer> map = new HashMap<>();
                map.put("role_id",roleId);
                map.put("permission_id",checkPermissionId);
                roleDao.setPermissionRole(map);
            }
        }
    }
    @Override
    public List<Role> findAllZP() {
        List<Role> list = roleDao.findAllZP();
        return list;
    }
}
